



<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#FFF">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon.png">

<link rel="icon" type="image/ico" sizes="32x32" href="/blog/images/favicon.ico">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">


<link rel="alternate" type="application/rss+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/rss.xml" />
<link rel="alternate" type="application/atom+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/atom.xml" />
<link rel="alternate" type="application/json" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/feed.json" />

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/blog/css/app.css?v=0.2.5">

  
  <meta name="keywords" content="分布式" />


<link rel="canonical" href="http://fyduyu.gitee.io/blog/2020/12/24/Hystrix/">



  <title>
Hystrix - SpringCloud |
blog = 凡尘一叶</title>
<meta name="generator" content="Hexo 5.3.0"></head>
<body itemscope itemtype="http://schema.org/WebPage">
  <div id="loading">
    <div class="cat">
      <div class="body"></div>
      <div class="head">
        <div class="face"></div>
      </div>
      <div class="foot">
        <div class="tummy-end"></div>
        <div class="bottom"></div>
        <div class="legs left"></div>
        <div class="legs right"></div>
      </div>
      <div class="paw">
        <div class="hands left"></div>
        <div class="hands right"></div>
      </div>
    </div>
  </div>
  <div id="container">
    <header id="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="inner">
        <div id="brand">
          <div class="pjax">
          
  <h1 itemprop="name headline">Hystrix
  </h1>
  
<div class="meta">
  <span class="item" title="创建时间：2020-12-24 00:49:34">
    <span class="icon">
      <i class="ic i-calendar"></i>
    </span>
    <span class="text">发表于</span>
    <time itemprop="dateCreated datePublished" datetime="2020-12-24T00:49:34+08:00">2020-12-24</time>
  </span>
  <span class="item" title="本文字数">
    <span class="icon">
      <i class="ic i-pen"></i>
    </span>
    <span class="text">本文字数</span>
    <span>11k</span>
    <span class="text">字</span>
  </span>
  <span class="item" title="阅读时长">
    <span class="icon">
      <i class="ic i-clock"></i>
    </span>
    <span class="text">阅读时长</span>
    <span>10 分钟</span>
  </span>
</div>


          </div>
        </div>
        <nav id="nav">
  <div class="inner">
    <div class="toggle">
      <div class="lines" aria-label="切换导航栏">
        <span class="line"></span>
        <span class="line"></span>
        <span class="line"></span>
      </div>
    </div>
    <ul class="menu">
      <li class="item title"><a href="/blog/" rel="start">blog</a></li>
    </ul>
    <ul class="right">
      <li class="item theme">
        <i class="ic i-sun"></i>
      </li>
      <li class="item search">
        <i class="ic i-search"></i>
      </li>
    </ul>
  </div>
</nav>

      </div>
      <div id="imgs" class="pjax">
        <ul>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicivghyooj20zk0m8dir.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gipevo9j1jj20zk0m8e81.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gipet4bz0yj20zk0m8e81.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicm0fdw5cj20zk0m8hdt.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1giclffsa1cj20zk0m811l.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicitht3xtj20zk0m8k5v.jpg"></li>
        </ul>
      </div>
    </header>
    <div id="waves">
      <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
        <defs>
          <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" />
        </defs>
        <g class="parallax">
          <use xlink:href="#gentle-wave" x="48" y="0" />
          <use xlink:href="#gentle-wave" x="48" y="3" />
          <use xlink:href="#gentle-wave" x="48" y="5" />
          <use xlink:href="#gentle-wave" x="48" y="7" />
        </g>
      </svg>
    </div>
    <main>
      <div class="inner">
        <div id="main" class="pjax">
          
  <div class="article wrap">
    
<div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
<i class="ic i-home"></i>
<span><a href="/blog/">首页</a></span><i class="ic i-angle-right"></i>
<span  class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/blog/categories/SpringCloud/" itemprop="item" rel="index" title="分类于 SpringCloud"><span itemprop="name">SpringCloud</span></a>
<meta itemprop="position" content="1" /></span>
</div>

    <article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN">
  <link itemprop="mainEntityOfPage" href="http://fyduyu.gitee.io/blog/2020/12/24/Hystrix/">

  <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="image" content="/blog/images/touxiang.jpg">
    <meta itemprop="name" content="杜朝宇">
    <meta itemprop="description" content=", 个人博客&编程笔记">
  </span>

  <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="凡尘一叶">
  </span>

  <div class="body md" itemprop="articleBody">
    

    <h1 id="服务降级"><a class="markdownIt-Anchor" href="#服务降级">#</a> 服务降级</h1>
<h2 id="服务端"><a class="markdownIt-Anchor" href="#服务端">#</a> 服务端</h2>
<p>1, 为 service 的指定方法 (会延迟的方法) 添加 @HystrixCommand 注解</p>
<p>paymentInfo_TimeOutHandler 为添加兜底方法 value 为超时的时间</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@HystrixCommand</span><span class="token punctuation">(</span>fallbackMethod <span class="token operator">=</span> <span class="token string">"paymentInfo_TimeOutHandler"</span><span class="token punctuation">,</span>commandProperties <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>        <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"execution.isolation.thread.timeoutInMilliseconds"</span><span class="token punctuation">,</span>value<span class="token operator">=</span><span class="token string">"3000"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>2, 主启动类上，添加激活 hystrix 的注解</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@EnableCircuitBreaker</span></pre></td></tr></table></figure><p>3, 触发异常也触发了降级</p>
<h2 id="客服端"><a class="markdownIt-Anchor" href="#客服端">#</a> 客服端</h2>
<p>1, 修改配置文件：修改了会超市 1s 会报错，不要修改</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">feign</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">hystrix</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr></table></figure><p>设置超时时间</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">ReadTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr></table></figure><p>2, 主启动类添加直接，启用 hystrix:</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@EnableHystrix</span></pre></td></tr></table></figure><p>3, 修改 controller, 添加降级方法</p>
<p>和服务端一样</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@HystrixCommand</span><span class="token punctuation">(</span>fallbackMethod <span class="token operator">=</span> <span class="token string">"paymentInfo_TimeOutHandler"</span><span class="token punctuation">,</span>commandProperties <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>        <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"execution.isolation.thread.timeoutInMilliseconds"</span><span class="token punctuation">,</span>value<span class="token operator">=</span><span class="token string">"3000"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span><span class="token punctuation">)</span></pre></td></tr></table></figure><h2 id="重构"><a class="markdownIt-Anchor" href="#重构">#</a> 重构:</h2>
<h3 id="上面出现的问题"><a class="markdownIt-Anchor" href="#上面出现的问题">#</a> 上面出现的问题:</h3>
<p>​		1, 降级方法与业务方法写在了一块，耦合度高</p>
<p>​		2. 每个业务方法都写了一个降级方法，重复代码多</p>
<h3 id="解决重复代码的问题"><a class="markdownIt-Anchor" href="#解决重复代码的问题">#</a> <strong>解决重复代码的问题</strong>:</h3>
<p>配置一个全局的降级方法，所有方法都可以走这个降级方法，至于某些特殊创建，再单独创建方法</p>
<p>1, 创建一个全局方法</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">payment_Global_FallBackMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">return</span> <span class="token string">"这里是全局异常处理方法"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>2, 使用注解指定其为全局降级方法 (默认降级方法)</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@DefaultProperties</span><span class="token punctuation">(</span>defaultFallback <span class="token operator">=</span> <span class="token string">"payment_Global_FallBackMethod"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token comment">// 加超时</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token annotation punctuation">@DefaultProperties</span><span class="token punctuation">(</span>defaultFallback <span class="token operator">=</span> <span class="token string">"payment_Global_FallBackMethod"</span><span class="token punctuation">,</span>commandProperties <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"execution.isolation.thread.timeoutInMilliseconds"</span><span class="token punctuation">,</span>value<span class="token operator">=</span><span class="token string">"3000"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>3, 业务方法使用默认降级方法:</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@HystrixCommand</span></pre></td></tr></table></figure><h3 id="解决代码耦合度的问题"><a class="markdownIt-Anchor" href="#解决代码耦合度的问题">#</a> 解决代码耦合度的问题:</h3>
<p>修改 order 模块，这里开始，pay 模块就不服务降级了，服务降级写在 order 模块即可</p>
<p>1,Payservice 接口是远程调用 pay 模块的，我们这里创建一个类实现 service 接口，在实现类中统一处理异常</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PaymentFallbackService</span> <span class="token keyword">implements</span> <span class="token class-name">PaymentHystrixService</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfo_OK</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"服务器死机了"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfo_TimeOut</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"服务器死机了"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>2, 修改配置文件：添加:</p>
<p>必须要</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">feign</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">hystrix</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr></table></figure><p>超时设置</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">hystrix</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">command</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">default</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>      <span class="token key atrule">execution</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token key atrule">isolation</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="6"></td><td><pre>          <span class="token key atrule">thread</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>            <span class="token key atrule">timeoutInMilliseconds</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>  <span class="token key atrule">ReadTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr><tr><td data-num="11"></td><td><pre>  <span class="token key atrule">ConnectTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr></table></figure><p>3, 让 PayService 的实现类生效:</p>
<p>注意：可能会超时</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">"CLOUD-PROVIDER-HYSTRIX-PAYMENT"</span><span class="token punctuation">,</span>fallback <span class="token operator">=</span> <span class="token class-name">PaymentFallbackService</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">PaymentHystrixService</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/hystrix/ok/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfo_OK</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/hystrix/timeout/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfo_TimeOut</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">"id"</span><span class="token punctuation">)</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h1 id="服务熔断"><a class="markdownIt-Anchor" href="#服务熔断">#</a> 服务熔断:</h1>
<p>大神论文:<span class="exturl" data-url="aHR0cHM6Ly9tYXJ0aW5mb3dsZXIuY29tL2JsaWtpL0NpcmN1aXRCcmVha2VyLmh0bWw=">https://martinfowler.com/bliki/CircuitBreaker.html</span></p>
<p>熔断机制是应对雪崩效应的一种微服务链路保护机制</p>
<p>当扇出链路的某个微服务不可用或者响应时间太长时，会进行服务的降级，进而熔断该节点微服务的调用，快速返回” 错误” 的响应信息。当检测到该节点微服务响应正常后恢复调用链路，在 SpringCloud 框架机制通过 Hystrix 实现，Hystrix 会监控微服务见调用的状况，当失败的调用到一个阈值，缺省是 5 秒内 20 次调用失败和失败率达到 50% 就会启动熔断机制，</p>
<h2 id="配置"><a class="markdownIt-Anchor" href="#配置">#</a> 配置</h2>
<p>熔断机制的注解是 @HystrixCommand</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">//processHystrixGet 是熔断后的方法</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@HystrixCommand</span><span class="token punctuation">(</span>fallbackMethod <span class="token operator">=</span> <span class="token string">"processHystrixGet"</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>主启动类添加注解 @EnableCircuitBreaker</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@EnableCircuitBreaker</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token comment">// 启用断路器 </span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.enabled"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"true"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment">// 设置请求次数 </span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.requestVolumeThreshold"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"10"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">// 设置时间窗口期 </span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.sleepWindowInMilliseconds"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"10000"</span><span class="token punctuation">)</span>`</pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment">// 设置失败率 </span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.errorThresholdPercentage"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"60"</span><span class="token punctuation">)</span></pre></td></tr></table></figure><figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@HystrixCommand</span><span class="token punctuation">(</span>fallbackMethod <span class="token operator">=</span> <span class="token string">"breakFallback"</span><span class="token punctuation">,</span> commandProperties <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>            <span class="token comment">// 开启断路器</span></pre></td></tr><tr><td data-num="3"></td><td><pre>            <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.enabled"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="4"></td><td><pre>            <span class="token comment">// 请求次数</span></pre></td></tr><tr><td data-num="5"></td><td><pre>            <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.requestVolumeThreshold"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"10"</span><span class="token punctuation">)</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="6"></td><td><pre>            <span class="token comment">// 时间窗口期</span></pre></td></tr><tr><td data-num="7"></td><td><pre>            <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.sleepWindowInMilliseconds"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"10000"</span><span class="token punctuation">)</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="8"></td><td><pre>            <span class="token comment">// 失败率</span></pre></td></tr><tr><td data-num="9"></td><td><pre>            <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"circuitBreaker.errorThresholdPercentage"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"60"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>如上设置的值，如果在 10 秒内，失败率达到请求次数（10）的百分之 60，也就是 6 次就会 打开断路器；否则断路器依然关闭</p>
<h2 id="熔断整体流程"><a class="markdownIt-Anchor" href="#熔断整体流程">#</a> <strong>熔断整体流程:</strong></h2>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token number">1</span>请求进来<span class="token punctuation">,</span>首先查询缓存<span class="token punctuation">,</span>如果缓存有<span class="token punctuation">,</span>直接返回</pre></td></tr><tr><td data-num="2"></td><td><pre>  	如果缓存没有<span class="token punctuation">,</span><span class="token operator">--</span><span class="token operator">-></span><span class="token number">2</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token number">2</span><span class="token punctuation">,</span>查看断路器是否开启<span class="token punctuation">,</span>如果开启的<span class="token punctuation">,</span><span class="token class-name">Hystrix</span>直接将请求转发到降级返回<span class="token punctuation">,</span>然后返回</pre></td></tr><tr><td data-num="4"></td><td><pre>  	如果断路器是关闭的<span class="token punctuation">,</span></pre></td></tr><tr><td data-num="5"></td><td><pre>				判断线程池等资源是否已经满了<span class="token punctuation">,</span>如果已经满了</pre></td></tr><tr><td data-num="6"></td><td><pre>  					也会走降级方法</pre></td></tr><tr><td data-num="7"></td><td><pre>  			如果资源没有满<span class="token punctuation">,</span>判断我们使用的什么类型的<span class="token class-name">Hystrix</span><span class="token punctuation">,</span>决定调用构造方法还是run方法</pre></td></tr><tr><td data-num="8"></td><td><pre>        然后处理请求</pre></td></tr><tr><td data-num="9"></td><td><pre>        然后<span class="token class-name">Hystrix</span>将本次请求的结果信息汇报给断路器<span class="token punctuation">,</span>因为断路器此时可能是开启的</pre></td></tr><tr><td data-num="10"></td><td><pre>          			<span class="token punctuation">(</span>因为断路器开启也是可以接收请求的<span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        		断路器收到信息<span class="token punctuation">,</span>判断是否符合开启或关闭断路器的条件<span class="token punctuation">,</span></pre></td></tr><tr><td data-num="12"></td><td><pre>				如果本次请求处理失败<span class="token punctuation">,</span>又会进入降级方法</pre></td></tr><tr><td data-num="13"></td><td><pre>        如果处理成功<span class="token punctuation">,</span>判断处理是否超时<span class="token punctuation">,</span>如果超时了<span class="token punctuation">,</span>也进入降级方法</pre></td></tr><tr><td data-num="14"></td><td><pre>        最后<span class="token punctuation">,</span>没有超时<span class="token punctuation">,</span>则本次请求处理成功<span class="token punctuation">,</span>将结果返回给controller</pre></td></tr></table></figure><h2 id="更多的hystrixcommand属性设置查看hystrixcommandproperties类"><a class="markdownIt-Anchor" href="#更多的hystrixcommand属性设置查看hystrixcommandproperties类">#</a> 更多的 @HystrixCommand 属性设置查看 HystrixCommandProperties 类</h2>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">protected</span> <span class="token class-name">HystrixCommandProperties</span><span class="token punctuation">(</span><span class="token class-name">HystrixCommandKey</span> key<span class="token punctuation">,</span> <span class="token class-name">HystrixCommandProperties</span><span class="token punctuation">.</span><span class="token class-name">Setter</span> builder<span class="token punctuation">,</span> <span class="token class-name">String</span> propertyPrefix<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>key <span class="token operator">=</span> key<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerRequestVolumeThreshold <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.requestVolumeThreshold"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerRequestVolumeThreshold</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerRequestVolumeThreshold<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerSleepWindowInMilliseconds <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.sleepWindowInMilliseconds"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerSleepWindowInMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerSleepWindowInMilliseconds<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerErrorThresholdPercentage <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.errorThresholdPercentage"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerErrorThresholdPercentage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerErrorThresholdPercentage<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerForceOpen <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.forceOpen"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerForceOpen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerForceOpen<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>circuitBreakerForceClosed <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"circuitBreaker.forceClosed"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getCircuitBreakerForceClosed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_circuitBreakerForceClosed<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionIsolationStrategy <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.isolation.strategy"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionIsolationStrategy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionIsolationStrategy<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionTimeoutInMilliseconds <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.isolation.thread.timeoutInMilliseconds"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionIsolationThreadTimeoutInMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionTimeoutInMilliseconds<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionTimeoutEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.timeout.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionTimeoutEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionTimeoutEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionIsolationThreadInterruptOnTimeout <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.isolation.thread.interruptOnTimeout"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionIsolationThreadInterruptOnTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionIsolationThreadInterruptOnTimeout<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionIsolationThreadInterruptOnFutureCancel <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.isolation.thread.interruptOnFutureCancel"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionIsolationThreadInterruptOnFutureCancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionIsolationThreadInterruptOnFutureCancel<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionIsolationSemaphoreMaxConcurrentRequests <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"execution.isolation.semaphore.maxConcurrentRequests"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getExecutionIsolationSemaphoreMaxConcurrentRequests</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_executionIsolationSemaphoreMaxConcurrentRequests<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>fallbackIsolationSemaphoreMaxConcurrentRequests <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"fallback.isolation.semaphore.maxConcurrentRequests"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getFallbackIsolationSemaphoreMaxConcurrentRequests</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_fallbackIsolationSemaphoreMaxConcurrentRequests<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>fallbackEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"fallback.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getFallbackEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_fallbackEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingStatisticalWindowInMilliseconds <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingStats.timeInMilliseconds"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingStatisticalWindowInMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingStatisticalWindow<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingStatisticalWindowBuckets <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingStats.numBuckets"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingStatisticalWindowBuckets</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingStatisticalWindowBuckets<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="19"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingPercentileEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingPercentile.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingPercentileEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingPercentileEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="20"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingPercentileWindowInMilliseconds <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingPercentile.timeInMilliseconds"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingPercentileWindowInMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingPercentileWindow<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="21"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingPercentileWindowBuckets <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingPercentile.numBuckets"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingPercentileWindowBuckets</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingPercentileWindowBuckets<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="22"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsRollingPercentileBucketSize <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.rollingPercentile.bucketSize"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsRollingPercentileBucketSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsRollingPercentileBucketSize<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="23"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>metricsHealthSnapshotIntervalInMilliseconds <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"metrics.healthSnapshot.intervalInMilliseconds"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getMetricsHealthSnapshotIntervalInMilliseconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_metricsHealthSnapshotIntervalInMilliseconds<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>requestCacheEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"requestCache.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getRequestCacheEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_requestCacheEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="25"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>requestLogEnabled <span class="token operator">=</span> <span class="token function">getProperty</span><span class="token punctuation">(</span>propertyPrefix<span class="token punctuation">,</span> key<span class="token punctuation">,</span> <span class="token string">"requestLog.enabled"</span><span class="token punctuation">,</span> builder<span class="token punctuation">.</span><span class="token function">getRequestLogEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> default_requestLogEnabled<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="26"></td><td><pre>        <span class="token keyword">this</span><span class="token punctuation">.</span>executionIsolationThreadPoolKeyOverride <span class="token operator">=</span> <span class="token class-name">HystrixPropertiesChainedProperty</span><span class="token punctuation">.</span><span class="token function">forString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>propertyPrefix <span class="token operator">+</span> <span class="token string">".command."</span> <span class="token operator">+</span> key<span class="token punctuation">.</span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">".threadPoolKeyOverride"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token class-name">Object</span><span class="token punctuation">)</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h1 id="hystrix服务监控"><a class="markdownIt-Anchor" href="#hystrix服务监控">#</a> Hystrix 服务监控:</h1>
<h2 id="使用hystrixdashboard"><a class="markdownIt-Anchor" href="#使用hystrixdashboard">#</a> 使用 HystrixDashboard:</h2>
<h2 id="创建项目"><a class="markdownIt-Anchor" href="#创建项目">#</a> 创建项目:</h2>
<p>名字: cloud-hystrixdashboard-9001</p>
<h2 id="pom文件"><a class="markdownIt-Anchor" href="#pom文件">#</a> pom 文件</h2>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span><span class="token comment">&lt;!-- 引用自己定义的 api 通用包，可以使用 Payment 支付 Entity --></span></pre></td></tr><tr><td data-num="3"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.eiletxie.springcloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>cloud-api-commons<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>$&#123;project.version&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="12"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="13"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-hystrix-dashboard<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token comment">&lt;!-- 监控 --></span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="17"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="18"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-actuator<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="19"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="20"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="21"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="22"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-hystrix<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="23"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="24"></td><td><pre>        <span class="token comment">&lt;!--eureka client--></span></pre></td></tr><tr><td data-num="25"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="26"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="27"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-eureka-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="28"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="29"></td><td><pre>        <span class="token comment">&lt;!-- 热部署 --></span></pre></td></tr><tr><td data-num="30"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="31"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="32"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-devtools<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="33"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>runtime<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="34"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>optional</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>optional</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="35"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="36"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="37"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="38"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="39"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>optional</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>optional</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="40"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="41"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="42"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="43"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="44"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="45"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="46"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h2 id="配置文件"><a class="markdownIt-Anchor" href="#配置文件">#</a> 配置文件</h2>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9001</span></pre></td></tr></table></figure><h2 id="主启动类"><a class="markdownIt-Anchor" href="#主启动类">#</a> 主启动类</h2>
<p>@EnableHystrixDashboard</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@EnableHystrixDashboard</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HystrixDashBoardMain9001</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">HystrixDashBoardMain9001</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h2 id="所有被监控的都需要一个pom依赖"><a class="markdownIt-Anchor" href="#所有被监控的都需要一个pom依赖">#</a> 所有被监控的都需要一个 pom 依赖</h2>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">&lt;!-- 监控 --></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-actuator<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h2 id="启动访问"><a class="markdownIt-Anchor" href="#启动访问">#</a> 启动访问:</h2>
<p>localhost:9001/hystrix</p>
<h2 id="注意如果你使用的是2x等比较新的版本需要在-hystrix-的消费端配置监控路径我们这里消费端是-spring-cloud-consul-consumer-所以修改它的启动类"><a class="markdownIt-Anchor" href="#注意如果你使用的是2x等比较新的版本需要在-hystrix-的消费端配置监控路径我们这里消费端是-spring-cloud-consul-consumer-所以修改它的启动类">#</a> 注意，如果你使用的是 2.x 等比较新的版本，需要在 Hystrix 的消费端配置监控路径，我们这里消费端是 spring-cloud-consul-consumer， 所以修改它的启动类。</h2>
<p>如果要监控，还需要配置：(8001 为例)</p>
<p><mark>8001 的主启动类添加:</mark></p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">// 此配置是为了服务监控而配置，与服务容错本身无关，</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token comment">// ServletRegistrationBean 因为 springboot 的默认路径不是 "/hystrix.stream"，</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token comment">// 只要在自己的项目里配置上下面的 servlet 就可以了</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token annotation punctuation">@Bean</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">ServletRegistrationBean</span> <span class="token function">getServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token class-name">HystrixMetricsStreamServlet</span> streamServlet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HystrixMetricsStreamServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token class-name">ServletRegistrationBean</span> registrationBean <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ServletRegistrationBean</span><span class="token punctuation">(</span>streamServlet<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        registrationBean<span class="token punctuation">.</span><span class="token function">setLoadOnStartup</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        registrationBean<span class="token punctuation">.</span><span class="token function">addUrlMappings</span><span class="token punctuation">(</span><span class="token string">"/hystrix.stream"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        registrationBean<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"HystrixMetricsStreamServlet"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token keyword">return</span> registrationBean<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h2 id="启动服务"><a class="markdownIt-Anchor" href="#启动服务">#</a> 启动服务:</h2>
<p><span class="exturl" data-url="aHR0cDovL2xvY2FsaG9zdDo4MDAxL2h5c3RyaXguc3RyZWFt">http://localhost:8001/hystrix.stream</span></p>

      <div class="tags">
          <a href="/blog/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" rel="tag"><i class="ic i-tag"></i> 分布式</a>
      </div>
  </div>

   <footer>

    <div class="meta">
  <span class="item">
    <span class="icon">
      <i class="ic i-calendar-check"></i>
    </span>
    <span class="text">更新于</span>
    <time title="修改时间：2020-12-24 10:59:29" itemprop="dateModified" datetime="2020-12-24T10:59:29+08:00">2020-12-24</time>
  </span>
  <span id="2020/12/24/Hystrix/" class="item leancloud_visitors" data-flag-title="Hystrix" title="阅读次数">
      <span class="icon">
        <i class="ic i-eye"></i>
      </span>
      <span class="text">阅读次数</span>
      <span class="leancloud-visitors-count"></span>
      <span class="text">次</span>
  </span>
</div>

      
<div class="reward">
  <button><i class="ic i-heartbeat"></i> 赞赏</button>
  <p>请我喝[茶]~(￣▽￣)~*</p>
  <div id="qr">
      
      <div>
        <img data-src="/blog/images/wechatpay.png" alt="杜朝宇 微信支付">
        <p>微信支付</p>
      </div>
      
      <div>
        <img data-src="/blog/images/alipay.png" alt="杜朝宇 支付宝">
        <p>支付宝</p>
      </div>
      
      <div>
        <img data-src="/blog/images/paypal.png" alt="杜朝宇 贝宝">
        <p>贝宝</p>
      </div>
  </div>
</div>

      

<div id="copyright">
<ul>
  <li class="author">
    <strong>本文作者： </strong>杜朝宇 <i class="ic i-at"><em>@</em></i>凡尘一叶
  </li>
  <li class="link">
    <strong>本文链接：</strong>
    <a href="http://fyduyu.gitee.io/blog/2020/12/24/Hystrix/" title="Hystrix">http://fyduyu.gitee.io/blog/2020/12/24/Hystrix/</a>
  </li>
  <li class="license">
    <strong>版权声明： </strong>本站所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

  </footer>

</article>

  </div>
  

<div class="post-nav">
    <div class="item left">
      

  <a href="/blog/2020/12/23/moti%E5%BC%80%E6%BA%90%E5%AD%A6%E5%88%B0%E7%9A%84/" itemprop="url" rel="prev" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1gipet4bz0yj20zk0m8e81.jpg" title="moti">
  <span class="type">上一篇</span>
  <span class="category"><i class="ic i-flag"></i> 开源项目笔记</span>
  <h3>moti</h3>
  </a>

    </div>
    <div class="item right">
      

  <a href="/blog/2020/12/24/gateway/" itemprop="url" rel="next" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1giclfw2t96j20zk0m8x6p.jpg" title="gateway">
  <span class="type">下一篇</span>
  <span class="category"><i class="ic i-flag"></i> SpringCloud</span>
  <h3>gateway</h3>
  </a>

    </div>
</div>

  
  <div class="wrap" id="comments"></div>


        </div>
        <div id="sidebar">
          

<div class="inner">

  <div class="panels">
    <div class="inner">
      <div class="contents panel pjax" data-title="文章目录">
          <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7"><span class="toc-number">1.</span> <span class="toc-text"> 服务降级</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E7%AB%AF"><span class="toc-number">1.1.</span> <span class="toc-text"> 服务端</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%A2%E6%9C%8D%E7%AB%AF"><span class="toc-number">1.2.</span> <span class="toc-text"> 客服端</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%87%8D%E6%9E%84"><span class="toc-number">1.3.</span> <span class="toc-text"> 重构:</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8A%E9%9D%A2%E5%87%BA%E7%8E%B0%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.3.1.</span> <span class="toc-text"> 上面出现的问题:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E5%86%B3%E9%87%8D%E5%A4%8D%E4%BB%A3%E7%A0%81%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.3.2.</span> <span class="toc-text"> 解决重复代码的问题:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E5%86%B3%E4%BB%A3%E7%A0%81%E8%80%A6%E5%90%88%E5%BA%A6%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.3.3.</span> <span class="toc-text"> 解决代码耦合度的问题:</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E7%86%94%E6%96%AD"><span class="toc-number">2.</span> <span class="toc-text"> 服务熔断:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE"><span class="toc-number">2.1.</span> <span class="toc-text"> 配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%86%94%E6%96%AD%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B"><span class="toc-number">2.2.</span> <span class="toc-text"> 熔断整体流程:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9B%B4%E5%A4%9A%E7%9A%84hystrixcommand%E5%B1%9E%E6%80%A7%E8%AE%BE%E7%BD%AE%E6%9F%A5%E7%9C%8Bhystrixcommandproperties%E7%B1%BB"><span class="toc-number">2.3.</span> <span class="toc-text"> 更多的 @HystrixCommand 属性设置查看 HystrixCommandProperties 类</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#hystrix%E6%9C%8D%E5%8A%A1%E7%9B%91%E6%8E%A7"><span class="toc-number">3.</span> <span class="toc-text"> Hystrix 服务监控:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8hystrixdashboard"><span class="toc-number">3.1.</span> <span class="toc-text"> 使用 HystrixDashboard:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="toc-number">3.2.</span> <span class="toc-text"> 创建项目:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pom%E6%96%87%E4%BB%B6"><span class="toc-number">3.3.</span> <span class="toc-text"> pom 文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">3.4.</span> <span class="toc-text"> 配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BB%E5%90%AF%E5%8A%A8%E7%B1%BB"><span class="toc-number">3.5.</span> <span class="toc-text"> 主启动类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%80%E6%9C%89%E8%A2%AB%E7%9B%91%E6%8E%A7%E7%9A%84%E9%83%BD%E9%9C%80%E8%A6%81%E4%B8%80%E4%B8%AApom%E4%BE%9D%E8%B5%96"><span class="toc-number">3.6.</span> <span class="toc-text"> 所有被监控的都需要一个 pom 依赖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E8%AE%BF%E9%97%AE"><span class="toc-number">3.7.</span> <span class="toc-text"> 启动访问:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E5%A6%82%E6%9E%9C%E4%BD%A0%E4%BD%BF%E7%94%A8%E7%9A%84%E6%98%AF2x%E7%AD%89%E6%AF%94%E8%BE%83%E6%96%B0%E7%9A%84%E7%89%88%E6%9C%AC%E9%9C%80%E8%A6%81%E5%9C%A8-hystrix-%E7%9A%84%E6%B6%88%E8%B4%B9%E7%AB%AF%E9%85%8D%E7%BD%AE%E7%9B%91%E6%8E%A7%E8%B7%AF%E5%BE%84%E6%88%91%E4%BB%AC%E8%BF%99%E9%87%8C%E6%B6%88%E8%B4%B9%E7%AB%AF%E6%98%AF-spring-cloud-consul-consumer-%E6%89%80%E4%BB%A5%E4%BF%AE%E6%94%B9%E5%AE%83%E7%9A%84%E5%90%AF%E5%8A%A8%E7%B1%BB"><span class="toc-number">3.8.</span> <span class="toc-text"> 注意，如果你使用的是 2.x 等比较新的版本，需要在 Hystrix 的消费端配置监控路径，我们这里消费端是 spring-cloud-consul-consumer， 所以修改它的启动类。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1"><span class="toc-number">3.9.</span> <span class="toc-text"> 启动服务:</span></a></li></ol></li></ol>
      </div>
      <div class="related panel pjax" data-title="系列文章">
        <ul>
          <li class="active"><a href="/blog/2020/12/24/Hystrix/" rel="bookmark" title="Hystrix">Hystrix</a></li><li><a href="/blog/2020/12/24/gateway/" rel="bookmark" title="gateway">gateway</a></li><li><a href="/blog/2020/12/24/Spring%20Config+Bus/" rel="bookmark" title="Spring Config+Bus">Spring Config+Bus</a></li><li><a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" rel="bookmark" title="Spring Cloud Stream">Spring Cloud Stream</a></li><li><a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" rel="bookmark" title="Spring Cloud sleuth">Spring Cloud sleuth</a></li>
        </ul>
      </div>
      <div class="overview panel" data-title="站点概览">
        <div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="image" itemprop="image" alt="杜朝宇"
      data-src="/blog/images/touxiang.jpg">
  <p class="name" itemprop="name">杜朝宇</p>
  <div class="description" itemprop="description">个人博客&编程笔记</div>
</div>

<nav class="state">
    <div class="item posts">
      <a href="/blog/archives/">
        <span class="count">8</span>
        <span class="name">文章</span>
      </a>
    </div>
    <div class="item categories">
      <a href="/blog/categories/">
        <span class="count">4</span>
        <span class="name">分类</span>
      </a>
    </div>
    <div class="item tags">
      <a href="/blog/tags/">
        <span class="count">2</span>
        <span class="name">标签</span>
      </a>
    </div>
</nav>

<div class="social">
</div>

<ul class="menu">
  
    
  <li class="item">
    <a href="/blog/" rel="section"><i class="ic i-home"></i>首页</a>
  </li>

    
  <li class="item">
    <a href="/blog/about/" rel="section"><i class="ic i-user"></i>关于</a>
  </li>

        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a>
  </li>

        
  <li class="item">
    <a href="/blog/categories/" rel="section"><i class="ic i-th"></i>分类</a>
  </li>

        
  <li class="item">
    <a href="/blog/tags/" rel="section"><i class="ic i-tags"></i>标签</a>
  </li>

  </ul>
        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-magic"></i>链环</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/friends/" rel="section"><i class="ic i-heart"></i>友链</a>
  </li>

  </ul>

</ul>

      </div>
    </div>
  </div>

  <ul id="quick">
    <li class="prev pjax">
        <a href="/blog/2020/12/23/moti%E5%BC%80%E6%BA%90%E5%AD%A6%E5%88%B0%E7%9A%84/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a>
    </li>
    <li class="up"><i class="ic i-arrow-up"></i></li>
    <li class="down"><i class="ic i-arrow-down"></i></li>
    <li class="next pjax">
        <a href="/blog/2020/12/24/gateway/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a>
    </li>
    <li class="percent"></li>
  </ul>
</div>


        </div>
        <div class="dimmer"></div>
      </div>
    </main>
    <footer id="footer">
      <div class="inner">
        <div class="widgets">
          
<div class="rpost pjax">
  <h2>随机文章</h2>
  <ul>
      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" title="Spring Cloud Stream">Spring Cloud Stream</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Hystrix/" title="Hystrix">Hystrix</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/Spring-Cloud-Alibaba/" title="分类于 Spring Cloud Alibaba">Spring Cloud Alibaba</a>
</div>

    <span><a href="/blog/2020/12/24/Nacos/" title="Cloud Alibaba Nacos">Cloud Alibaba Nacos</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/java/" title="分类于 java">java</a>
</div>

    <span><a href="/blog/2020/12/23/Druid%E9%85%8D%E7%BD%AE/" title="Druid配置">Druid配置</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Config+Bus/" title="Spring Config+Bus">Spring Config+Bus</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B0/" title="分类于 开源项目笔记">开源项目笔记</a>
</div>

    <span><a href="/blog/2020/12/23/moti%E5%BC%80%E6%BA%90%E5%AD%A6%E5%88%B0%E7%9A%84/" title="moti">moti</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/gateway/" title="gateway">gateway</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" title="Spring Cloud sleuth">Spring Cloud sleuth</a></span>
  </li>

  </ul>
</div>
<div>
  <h2>最新评论</h2>
  <ul class="leancloud-recent-comment"></ul>
</div>

        </div>
        <div class="status">
  <div class="copyright">
    
    &copy; 2010 – 
    <span itemprop="copyrightYear">2020</span>
    <span class="with-love">
      <i class="ic i-sakura rotate"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">杜朝宇 @ blog</span>
  </div>
  <div class="count">
    <span class="post-meta-item-icon">
      <i class="ic i-chart-area"></i>
    </span>
    <span title="站点总字数">42k 字</span>

    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="ic i-coffee"></i>
    </span>
    <span title="站点阅读时长">38 分钟</span>
  </div>
  <div class="powered-by">
    基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span>
  </div>
</div>

      </div>
    </footer>
  </div>
<script data-config type="text/javascript">
  var LOCAL = {
    path: '2020/12/24/Hystrix/',
    favicon: {
      show: "（●´3｀●）やれやれだぜ",
      hide: "(´Д｀)大変だ！"
    },
    search : {
      placeholder: "文章搜索",
      empty: "关于 「 ${query} 」，什么也没搜到",
      stats: "${time} ms 内找到 ${hits} 条结果"
    },
    valine: true,fancybox: true,copyright: '复制成功，转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',
    ignores : [
      function(uri) {
        return uri.includes('#');
      },
      function(uri) {
        return new RegExp(LOCAL.path+"$").test(uri);
      }
    ]
  };
</script>

<script src="https://cdn.polyfill.io/v2/polyfill.js"></script>

<script src="//cdn.jsdelivr.net/combine/npm/pace-js@1.0.2/pace.min.js,npm/pjax@0.2.8/pjax.min.js,npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js,npm/animejs@3.2.0/lib/anime.min.js,npm/algoliasearch@4/dist/algoliasearch-lite.umd.js,npm/instantsearch.js@4/dist/instantsearch.production.min.js,npm/lozad@1/dist/lozad.min.js,npm/quicklink@2/dist/quicklink.umd.js"></script>

<script src="/blog/js/app.js?v=0.2.5"></script>




</body>
</html>
